package it.trekker.tests.online;

import java.util.List;
import it.trekker.dao.Excursion;
import it.trekker.dao.ExcursionCoordinate;
import org.junit.Assert;
import org.junit.Test;

public class ExcursionDAOTest extends TestCommons {
	
	
	
	@Test
	public void insertExcursionTest() {
		
		Excursion excursion = insertAnExcursion("Prova");
		
		Assert.assertTrue(excursion.getId() > 0);
		
		Excursion retrieved = excursionDAO.findExcursionById(excursion.getId());
		Assert.assertNotNull(retrieved);
		
	}
	
	@Test
	public void removeExcursionTest() {
		Excursion excursion = insertAnExcursion("Prova2");
		
		Assert.assertTrue(excursion.getId() > 0);
		
		excursionDAO.remove(excursion.getId());
		
		Excursion retrieved = excursionDAO.findExcursionById(excursion.getId());
		Assert.assertNull(retrieved);
	}
	
	@Test
	public void findCloseExcursionsTest() {
		
		Excursion excursion1 = insertAnExcursion("ProvaEscursionePerVicine");
		
		ExcursionCoordinate coord1 = insertACoordinate(excursion1, 45.2239, 10.7507);
		
		Assert.assertTrue(coord1.getId() > 0);
		
		ExcursionCoordinate retrieved1 = excursionCoordinateDAO.findCoordinateById(coord1.getId());
		Assert.assertNotNull(retrieved1);
		
		Excursion excursion2 = insertAnExcursion("ProvaVicina");
		
		ExcursionCoordinate coord2 = insertACoordinate(excursion2, 45.2249, 10.7517);
		
		Assert.assertTrue(coord2.getId() > 0);
		
		ExcursionCoordinate retrieved2 = excursionCoordinateDAO.findCoordinateById(coord2.getId());
		Assert.assertNotNull(retrieved2);
		
		List<Excursion> closeExcursions = excursionDAO.findCloseExcursions(excursion1, 10f);
		Assert.assertNotNull(closeExcursions);
		Assert.assertTrue(closeExcursions.size() > 0);
		
		logExcursions(closeExcursions);
	}
	
	@Test
	public void findInCacheTest() {
		
		// Inserting excursion and coordinates
		
		Excursion excursion1 = insertAnExcursion("ProvaEscursionePerVicine");
		ExcursionCoordinate coord1 = insertACoordinate(excursion1, 45.2239, 10.7507);
		
		Excursion excursion2 = insertAnExcursion("ProvaVicina");
		ExcursionCoordinate coord2 = insertACoordinate(excursion2, 45.2249, 10.7517);
		
		
		// Retrieving excursion1 (from database)
		
		Excursion excursion1Retrieved = excursionDAO.findExcursionById(excursion1.getId());
		Assert.assertNotNull(excursion1Retrieved);
		
		
		// Retrieving excursion2 (from cache)
		
		Excursion excursion2Retrieved = excursionDAO.findExcursionById(excursion2.getId());
		Assert.assertNotNull(excursion2Retrieved);
	}
}
